package com.hzw.saas.service.schedule.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.schedule.IUserScheduleGroupService;
import com.hzw.saas.api.schedule.bo.UserScheduleGroupBo;
import com.hzw.saas.api.schedule.query.UserScheduleGroupQuery;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.schedule.constants.UserScheduleConst;
import com.hzw.saas.service.schedule.mapper.UserScheduleGroupMapper;
import com.hzw.saas.service.schedule.model.UserSchedule;
import com.hzw.saas.service.schedule.model.UserScheduleGroup;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author hankun
 */
@Service
public class UserScheduleGroupServiceImpl extends ServiceImpl<UserScheduleGroupMapper, UserScheduleGroup> implements IService<UserScheduleGroup>, IUserScheduleGroupService {

    static {
        MapperUtil.nfFactory()
                .classMap(UserScheduleGroup.class, UserScheduleGroupBo.class)
                .field("pid", "groupId")
                .byDefault()
                .register();
    }

    @Resource
    private UserScheduleServiceImpl userScheduleService;

    @Override
    public List<UserScheduleGroupBo> addGroup(UserScheduleGroupQuery userScheduleGroupQuery) {
        String userId = userScheduleGroupQuery.getUserId();
        UserScheduleGroup userScheduleGroup = MapperUtil.nf().map(userScheduleGroupQuery, UserScheduleGroup.class);
        userScheduleGroup.setPid(SnowFlakeUtil.nextIdStr());
        userScheduleGroup.setSortOrder(getNextSortOrder(userId));
        super.save(userScheduleGroup);
        return listGroup(userId);
    }

    @Override
    public List<UserScheduleGroupBo> updateGroup(UserScheduleGroupQuery userScheduleGroupQuery) {
        UserScheduleGroup userScheduleGroup = MapperUtil.nf().map(userScheduleGroupQuery, UserScheduleGroup.class);
        userScheduleGroup.setPid(userScheduleGroupQuery.getGroupId());
        super.updateById(userScheduleGroup);
        return listGroup(userScheduleGroupQuery.getUserId());
    }

    @Override
    public List<UserScheduleGroupBo> deleteGroup(UserScheduleGroupQuery userScheduleGroupQuery) {
        super.removeById(userScheduleGroupQuery.getGroupId());
        // 日程任务置为未分组
        userScheduleService.lambdaUpdate()
                .eq(UserSchedule::getGroupId, userScheduleGroupQuery.getGroupId())
                .set(UserSchedule::getGroupId, StrUtil.EMPTY)
                .update();
        return listGroup(userScheduleGroupQuery.getUserId());
    }

    @Override
    public List<UserScheduleGroupBo> listGroup(String userId) {
        return this.baseMapper.listGroup(userId, UserScheduleGroup.DEFAULT_GROUP);
    }

    private Long getNextSortOrder(String userId) {
        UserScheduleGroup oneGroup = super.lambdaQuery()
                .eq(UserScheduleGroup::getUserId, userId)
                .orderByDesc(UserScheduleGroup::getSortOrder)
                .last("limit 1")
                .one();
        return Objects.isNull(oneGroup) ? 0 : oneGroup.getSortOrder() + UserScheduleConst.INTERVAL_SORT;
    }
}
